package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Knowledge;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.UUIDGenerator;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Description: 知识页业务
 * @author: Rock
 * @time: 2020/10/28 11:22
 */
@Service
public class KnowledgeService {

    @Resource(name = "appMongoTemplate")
    private MongoTemplate mongoTemplate;

    @Resource
    private UserService userService;
    @Resource
    private OperationService operationService;

    /**
     * @param id
     * @time: 2020/10/22 11:42
     * @author: Rock
     * @version 1.0
     */
    public Knowledge getById(String id) {
        return mongoTemplate.findById(id, Knowledge.class);
    }

    /**
     * @param name
     * @time: 2020/10/27 9:13
     * @author: Rock
     * @version 1.0
     */
    public Knowledge getByName(String name) {
        return mongoTemplate.findOne(new Query(Criteria.where("name").is(name).and("deleted").is(0)), Knowledge.class);
    }

    /**
     * @param page
     * @time: 2020/10/27 8:49
     * @author: Rock
     * @version 1.0
     */
    public Page<Knowledge> list(PageVO page) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("create_time")));
        pageable.setPage(page);
        Criteria criteria = new Criteria();
        criteria.and("deleted").is(0);
        if (StringUtil.isNotBlank(page.getSearchValue())) {
            criteria.and("name").regex(".*?" + page.getSearchValue() + ".*?", "i");
        }
        Query query = new Query(criteria);
        long count = mongoTemplate.count(query, Knowledge.class);
        page.setTotalResult((int) count);
        List<Knowledge> list = mongoTemplate.find(query.with(pageable), Knowledge.class);
        for (Knowledge kn : list) {
            kn.setUser(userService.findById(kn.getCreator()));
        }
        Page<Knowledge> result = new PageImpl<Knowledge>(list, pageable, count);
        return result;
    }

    /**
     * 添加广告商
     *
     * @param knowledge
     * @param user
     * @param ip
     * @time: 2020/10/27 8:49
     * @author: Rock
     * @version 1.0
     */
    public void add(Knowledge knowledge, SysUser user, String ip) {
        if (knowledge != null && getByName(knowledge.getName()) == null) {
            knowledge.setId(UUIDGenerator.generate());
            knowledge.setDeleted(0);
            knowledge.setCreateTime(new Date());
            mongoTemplate.save(knowledge);
            operationService.add(user, ip, "知识页管理", "添加",
                    String.format("添加知识页: %s ", knowledge.getName()));
        }
    }

    /**
     * @param knowledge
     * @param user
     * @param ip
     * @time: 2020/10/27 8:48
     * @author: Rock
     * @version 1.0
     */
    public void update(Knowledge knowledge, SysUser user, String ip) {
        Query query = new Query(Criteria.where("_id").is(knowledge.getId()));
        Update update = new Update();
        update.set("name", knowledge.getName());
        update.set("index", knowledge.getIndex());
        if (StringUtil.isNotBlank(knowledge.getWebUrl())) {
            update.set("web_url", knowledge.getWebUrl());
        }
        if (StringUtil.isNotBlank(knowledge.getSdkMark())) {
            update.set("sdk_mark", knowledge.getSdkMark());
        }
        update.set("update_time", new Date());
        mongoTemplate.updateFirst(query, update, Knowledge.class);
        operationService.add(user, ip, "知识页管理", "编辑",
                String.format("编辑知识页: %s ", knowledge.getName()));
    }

    /**
     * @param id
     * @param user
     * @param ip
     * @time: 2020/10/23 9:32
     * @author: Rock
     * @version 1.0
     */
    public Response<Object> updateDel(String id, SysUser user, String ip) {
        Query query = new Query(Criteria.where("_id").is(id));
        Knowledge knowledge = mongoTemplate.findOne(query, Knowledge.class);
        if (knowledge != null) {
            Update update = new Update();
            update.set("deleted", 1);
            update.set("update_time", new Date());
            mongoTemplate.updateFirst(query, update, Knowledge.class);
            operationService.add(user, ip, "知识页管理", "删除",
                    String.format("删除知识页: %s ", knowledge.getName()));
            return Response.successResponse();
        }
        return Response.failedResponse();
    }

}
